#include "mainwindow.h"
#include "appsettings.h"
#include "qedaversion.h"
#include "qlib/librarymodel.h"
#include "qlib/qedalibrary.h"
#include "qlib/qedacomponent.h"
#include "qlib/componentwindow.h"

#include <QtCore/qdatetime.h>
#include <QtCore/qdebug.h>
#include <QtGui/qaction.h>
#include <QtGui/qmdiarea.h>
#include <QtGui/qmdisubwindow.h>
#include <QtGui/qmessagebox.h>
#include <QtGui/qpushbutton.h>
#include <QtGui/qtabbar.h>

void MainWindow::componentEdit(QedaComponent *component)
{
    QMdiSubWindow *subWin = findComponentWin(component);
    if (!subWin)
    {
        QMdiSubWindow *subWin = m_mdiArea->addSubWindow(new ComponentWindow(m_libModel, component));
        subWin->setWindowTitle(component->displayName());
        subWin->setWindowIcon(QIcon(":/images/component.png"));
        connect(component, SIGNAL(displayNameUpdated(QString)),
                subWin, SLOT(setWindowTitle(QString)));
        connect(subWin->widget(), SIGNAL(modified(bool)), this, SLOT(changeTabColor(bool)));
        component->setWin(subWin);
        subWin->show();
    }
    m_mdiArea->setActiveSubWindow(subWin);
}

void MainWindow::fileOpen()
{

}

void MainWindow::fileSave()
{
    for (int i = 0; i < m_libModel->libraries().count(); ++i)
    {
        QedaLibrary *lib = m_libModel->libraries().at(i);
        if (!lib->save())
        {
            QMessageBox::critical(this, tr("Library saving error"),
                                  tr("There is error when saving library:") +
                                  "<b>\"" + lib->name() + "\"</b>");
        }
    }
}

void MainWindow::helpAbout()
{
    QDate today = QDate::currentDate();
    const QString description = tr(
                "<h3><b>QEDA</b> is free open source electronic design automation software</h3>"
                "Version: <b>%1</b><br/><br/>"
                "Senior developer: <b>Shamil Yakupov</b> (<a href='mailto:shamil@qeda.org'>shamil@qeda.org</a>)<br/>"
                "Icons by &copy; <b>Mark James</b> (<a href='http://www.famfamfam.com/lab/icons/silk/'>www.famfamfam.com/lab/icons/silk</a>)<br/><br/>"
                "Copyright &copy; 2011&#150;%2 <b>qeda.org</b><br/>"
                "<table border=0>"
                "<tr><td>Web:</td><td><a href='http://qeda.org/'>qeda.org</a></td></tr>"
                "<tr><td>Latest release:</td><td><a href='http://qeda.org/latest/'>qeda.org/latest</a></td></tr>"
                "<tr><td>Source code:</td><td><a href='http://qeda.googlecode.com'>qeda.googlecode.com</a></td></tr>"
                "<tr><td>License:</td><td><a href='http://www.gnu.org/licenses/gpl.html'>GNU GPL v3</a></td></tr>"
                "</table>"
                ).arg(QEDA_VER_STR4).arg(today.year());
    QMessageBox::about(this, tr("About QEDA"), description);
}

void MainWindow::changeTabColor(bool redFlag)
{
    ComponentWindow *w = qobject_cast<ComponentWindow*>(sender());
    if (w)
    {
        QMdiSubWindow *subWin = qobject_cast<QMdiSubWindow*>(w->parentWidget());
        if (subWin)
        {
            int i = m_mdiArea->subWindowList().indexOf(subWin);
            if (i >= 0)
                m_tabBar->setTabTextColor(i, redFlag ? Qt::red : Qt::black);
        }
    }
}

void MainWindow::viewLanguage()
{
    QAction *src = qobject_cast<QAction*>(sender());
    if (!src)
        return;

    QMessageBox::information(this, tr("Need restart"),
                             tr("The application need to be restarted to take effect."));

    QString suffix = src->data().toString().toLower().simplified();
    g_appSettings->set("View/Language", suffix);
}
